package com.example.demo.aspect;

import com.example.demo.aop.DemoAopAnno;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

/**
 * @Author: zhangyang
 * @Date: 2020/4/18 18:45
 */
@Aspect
@Component
public class DemoAspect {
    @Pointcut("@annotation(com.example.demo.aop.DemoAopAnno)")
    public void annotationPointcut() {
    }

    //@Before 在注解添加的方法（此处定义为METHOD）执行之前执行
    @Before("annotationPointcut()")
    public void beforePointcut(JoinPoint joinPoint) throws Exception {
        // 此处进入到方法前
        DemoAopAnno demoAopAnno = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(DemoAopAnno.class);
        System.out.println("起床，前往" + demoAopAnno.company());
    }

    /*@AfterThrowing(throwing="ex", pointcut="within(com.example.demo.aop.*)")
    public void doAround(Throwable ex) throws Throwable {
        System.out.println("别担心，不懂就问才能百战百胜");
    }*/
    //@AfterReturning 在注解添加的方法（此处定义为METHOD）执行之后执行
    @AfterReturning("annotationPointcut()")
    public void doAfterReturning(JoinPoint joinPoint) {
        System.out.println("结束一天的工作，下班肥家");
        Field[] declaredFields = DemoAopAnno.class.getDeclaredFields();
    }
}
